package logic;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.game.Sprite;

/**
 * class with global variables and useful 'util' methods
 * @author Rekin
 *
 */
public class LogicHelper {
	
	public static final int menuItemsNumber = 6;
	public static final int gameTypeSinglePlayer = 11;
	public static final int gameTypeMultiPlayerLocal = 12;
	public static final int gameTypeMultiPlayerRemote = 13;
	public static final String defaultPlayerName = "Reyalp Tsrif";
	public static final int MAYOR_DEVICE_CLASS_PHONE = 0x0200;
	public static final int SERVICE_NAME_PARAM = 0x0100;
	public static final String serviceUUID = "20000000000010008000006057028C19";
	public static final int serviceAttributes[] = { SERVICE_NAME_PARAM };
	
	public static final int menuItemWidth = 140;
	public static final int menuItemHeight = 35;
	public static final int menuBaselineOffset = 20;
	
	public static final int menuItemTypeConnectionScreen = 100;
	public static final int menuItemTypeMainMenuScreen = 101;
	public static final int serverWaitingScreen = 102;
	public static final int clientWaitingScreen = 103;
	
	public static final int cardSelectPositionChange = 10;
	public static final int cardsHorisontalSpacing = 5;
	public static final int cardWidth = 50;
	public static final int cardHeight = 70;
	
	public static final int nokia5800NormalWidth = 350;
	public static final int nokia5800NormalHeight = 480;
	public static final int nokia5800LandscapeWidth = 500;
	public static final int nokia5800LandscapeHeight = 280;
	
	public static final int pressButtonCall = 201;
	public static final int pressButtonDouble = 202;
	public static final int pressButtonExchange = 203;
	public static final int pressButtonDrop = 204;
	public static final int pressButtonNext = 205;
	
	public static final int roundsNumber = 4;
	
	
	/**
	 * method for creating image from resource file
	 * @param imageFilePath relative path in res directory
	 * @param callingClass class for which image will be created
	 * @return created Image
	 */
	public static Image createImageFromResources(String imageFilePath, Object callingClass) {
		Image im = null;
		try {
			im = Image.createImage(callingClass.getClass().getResourceAsStream(imageFilePath));
		} 
		catch(NullPointerException e){
			System.err.println("Null pointer - resource invalid error");
		}
		catch (IOException e) {
			System.err.println("Can't load image - IO error");
		}
		return im;
	}
	
	/**
	 * check if pointer press was within the give Sprite
	 * @param sp given Sprite object
	 * @param pressed point where pointer was pressed
	 * @return true if given Sprite was hit, false otherwise
	 */
	public static boolean checkClickInside(Sprite sp, Point pressed){
		int spx1=sp.getX();
		int spy1=sp.getY();
		int spx2=sp.getWidth()+spx1;
		int spy2=sp.getHeight()+spy1;
		int px=pressed.getX();
		int py=pressed.getY();
		if(px>=spx1 && px<=spx2 && py>=spy1 && py<=spy2){
			return true;
		}
		return false;
	}
	
	/**
	 * creates StringItem object centered on the screen
	 * @param name label for item
	 * @param stringItemType type of item (Button, Text etc.)
	 * @return created StringItem
	 */
	public static StringItem createStringItemCentered(String name, int stringItemType){
		StringItem item;
		item=new StringItem("", name, stringItemType);
		item.setLayout(StringItem.LAYOUT_CENTER |
				StringItem.LAYOUT_VCENTER | StringItem.LAYOUT_NEWLINE_AFTER);
		return item;
	}
	
	/**
	 * method for making alerts
	 * @param title alert title
	 * @param text text of alert
	 * @param duration duration of alert
	 * @param type type of alert
	 * @return created Alert object
	 */
	public static Alert makePopup(String title, String text, int duration, AlertType type){
		Alert newPopup= new Alert(title, text,
                null, type);
		if(duration==-1){
			newPopup.setTimeout(Alert.FOREVER);
		}
		else{
			newPopup.setTimeout(duration);
		}
        return newPopup;
	}
	
	
	/**
	  * This method resizes an image by resampling its pixels
	  * @param src The image to be resized
	  * @return The resized image
	  */

	  public static Image resizeImage(Image src, int newWidth, int newHeight) {
	      int srcWidth = src.getWidth();
	      int srcHeight = src.getHeight();
	      Image tmp = Image.createImage(srcWidth, srcHeight);
	      Graphics g = tmp.getGraphics();
	      int ratio = (srcWidth << 16) / newWidth;
	      int pos = ratio/2;

	      //Horizontal Resize        
	      for (int x = 0; x < newWidth; x++) {
	          g.setClip(x, 0, 1, srcHeight);
	          g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
	          pos += ratio;
	      }

	      Image resizedImage = Image.createImage(newWidth, newHeight);
	      g = resizedImage.getGraphics();
	      ratio = (srcHeight << 16) / newHeight;
	      pos = ratio/2;        

	      //Vertical resize
	      for (int y = 0; y < newHeight; y++) {
	          g.setClip(0, y, newWidth, 1);
	          g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
	          pos += ratio;
	      }
	      return resizedImage;

	  }
}
